package sn_gm_report

import org.DataUtils

class DwsReworkController {

    def exportFileService
    def commonDataService
    def dynamicQueryService

//    def index(Integer max) {
//        def cellList = org.kdx.DataUtils.getCellList()
//        def pageSizeList = org.kdx.DataUtils.getPageSizeList()
//
//        [totalCount: 5, pageSize: 20, pageCurrent: 1, orderField: 'id', orderDirection: 'asc', pageSizeList: pageSizeList, cellList: cellList]
//    }

    def Report_084() {
        //��ҳ
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }

        params.SYS_QUERY_NAME = 'reworkphoto'
        def list = dynamicQueryService.queryForList(params)

        BigDecimal zero = new BigDecimal(0);
        BigDecimal totalReworkWafer = zero;
        BigDecimal totalReworkLot = zero;
        BigDecimal photoMoveWafer = zero;
        BigDecimal photoMoveLot = zero;
        BigDecimal ratioWafer = zero;
        BigDecimal ratioLot = zero;

        boolean isfirst = true;
        for (HashMap mp : list) {
            if (isfirst && list.size() > 1) {
                totalReworkWafer = new BigDecimal(mp.get("RWQTY").toString());
                totalReworkLot = new BigDecimal(mp.get("RWLOT").toString());
                isfirst = false;
            } else {
                photoMoveWafer = new BigDecimal(mp.get("RWQTY").toString());
                photoMoveLot = new BigDecimal(mp.get("RWLOT").toString());
            }
            if (list.size() == 1) {
                totalReworkWafer = new BigDecimal(0);
                totalReworkLot = new BigDecimal(0);
            }
        }
        BigDecimal hundred = new BigDecimal(100);
        ratioWafer = photoMoveWafer.compareTo(zero) == 0 ? zero : totalReworkWafer.multiply(hundred).divide(photoMoveWafer, 2, BigDecimal.ROUND_HALF_UP);
        ratioLot = photoMoveLot.compareTo(zero) == 0 ? zero : totalReworkLot.multiply(hundred).divide(photoMoveLot, 2, BigDecimal.ROUND_HALF_UP);

        HashMap row1 = new HashMap();
        HashMap row2 = new HashMap();
        list.clear();
        row1.put("TOTALWAFER", totalReworkWafer);
        row1.put("PMWafer", photoMoveWafer);
        row1.put("RADIOWAFER", ratioWafer);
        list.add(row1);
        row2.put("TOTALWAFER", totalReworkLot);
        row2.put("PMWafer", photoMoveLot);
        row2.put("RADIOWAFER", ratioLot);
        list.add(row2);



        params.SYS_QUERY_NAME = 'Report_084'
        def Report_084List = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (Report_084List.size() > 0) {
            tableHeadList = Report_084List[0].keySet()
        }
        params.SYS_QUERY_NAME = 'Report_084Count'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT

        //����
        if (params.export == 'xls') {
            def str = g.render(template: "${params.EXPORT_CONTENT_NAME}", model: [Report_084List: Report_084List, tableHeadList: tableHeadList, reworkphoto: list])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)
        } else {
            [pageSizeList: DataUtils.getPageSizeList(), pageSize: pageSize, startno: params.startno ?: '', endno: params.endno ?: '',
             totalCount  : totalCount, tableHeadList: tableHeadList, Report_084List: Report_084List, reworkphoto: list,
             lotId       : params.lotId ?: '', startTime: params.startTime ?: '',
             endTime     : params.endTime ?: '', lotId: params.lotId ?: '', partDesc: params.partDesc ?: '']
        }
    }

    def Report_084step() {
        params.SYS_QUERY_NAME = 'Report_084img'
        def Report_084img = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'Report_084sumQty'
        def Report_084sumQty = dynamicQueryService.queryForList(params)
        List<Integer> list1 = new ArrayList<Integer>();
        int n = 100;
        int sum = 0;
        BigDecimal ration1 = 0;
        BigDecimal ration2 = 0;
        //��rework�İٷֱȴ���list����
        for (int i = 0; i <= Report_084img.size() - 1; i++) {
            if (i == 0) {
                ration1 = Report_084img[i].REWORK_QTY / Report_084sumQty[0].SUM_QTY * 100
//                String str = ration1.toString();
//                str = str.substring(0, 5)
//                double str =(double)Math.round(ration1)
//                def str =new BigDecimal(Math.round(ration1))
                BigDecimal b = new BigDecimal(new Double(ration1).toString);
                double str = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                list1.add(i, str)

            } else {
                //sum����Ϊ0
                sum = 0;
                for (int j = 0; j <= i; j++) {
                    sum = sum + Report_084img[j].REWORK_QTY
                }
                ration2 = sum / Report_084sumQty[0].SUM_QTY * 100
//                String str = ration2.toString();
//                str = str.substring(0, 3)
//                double str =(double)Math.round(ration2)

                BigDecimal b = new BigDecimal(new Double(ration2).toString);
                double str = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
//                def str =new BigDecimal(Math.round(ration2))
                list1.add(i, str)
            }
        }
        [Report_084img: Report_084img, list: list1, lotId: params.lotId ?: '']
    }

    def Report_084stepTwo() {
        //��ҳ
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }


        params.SYS_QUERY_NAME = 'Report_084stepTwo'
        def Report_084stepTwo = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'Report_084stepTwoCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        def tableHeadList = []
        if (Report_084stepTwo.size() > 0) {
            tableHeadList = Report_084stepTwo[0].keySet()
        }

        print params
        //����
        if (params.export == 'xls') {
            def str = g.render(template: "${params.EXPORT_CONTENT_NAME}", model: [Report_084stepTwo: Report_084stepTwo, tableHeadList: tableHeadList])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)
        } else {
            [pageSizeList : DataUtils.getPageSizeList(), pageSize: pageSize, pageCurrent: pageCurrent, totalCount: totalCount,
             tableHeadList: tableHeadList, Report_084stepTwo: Report_084stepTwo, startTime: params.startTime ?: '',
             endTime      : params.endTime ?: '', lotId: params.lotId ?: '', partDesc: params.partDesc ?: '', reworkCode: params.reworkCode ?: '']
        }
    }

    def Report_084stepThree() {
        //��ҳ
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        if (params.export == 'xls') {
            params.startno = 1
            params.endno = 56800
        } else {
            params.startno = (pageSize * (pageCurrent - 1)) + 1
            params.endno = pageSize * pageCurrent
        }


        params.SYS_QUERY_NAME = 'Report_084stepThree'
        def Report_084stepThree = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'Report_084stepThreeCount'
        def totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        def tableHeadList = []
        if (Report_084stepThree.size() > 0) {
            tableHeadList = Report_084stepThree[0].keySet()
        }
        print(params)
        //����
        if (params.export == 'xls') {
            def str = g.render(template: "${params.EXPORT_CONTENT_NAME}", model: [Report_084stepThree: Report_084stepThree, tableHeadList: tableHeadList])
            exportFileService.exportExcelbyWeb(response, "${params.SYS_QUERY_NAME}", str)
        } else {
            [pageSizeList : DataUtils.getPageSizeList(), pageSize: pageSize, pageCurrent: pageCurrent, totalCount: totalCount,
             tableHeadList: tableHeadList, Report_084stepThree: Report_084stepThree, startTime: params.startTime ?: '', lotId: params.lotId ?: '',
             endTime      : params.endTime ?: '', lotId: params.lotId ?: '', partDesc: params.partDesc ?: '', reworkCode: params.reworkCode ?: '']
        }
    }
}